import java.sql.*;
public class JdbcTest03 {
    public static void main (String []args) {
//遍历结果集 -- JDBC
        Connection cnn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            //注册驱动
            Class.forName("com.mysql.jdbc.Driver");
            //获取连接对象
            cnn = DriverManager.getConnection("jdbc:mysql://localhost:3306/hjy","root","123456");
            System.out.println(cnn);
            //创建数据库操作对象
            ps = cnn.prepareStatement ("select * from emp where job = ?");
            System.out.println(ps);
            //执行sql语句
            ps.setString(1,"manager");
             rs = ps.executeQuery();
            System.out.println(rs);

            //处理结果集

            while(rs.next()){
                System.out.println(rs.getString("ename") +","+rs.getDouble("sal")+"," + rs.getString("job"));
            }

        } catch(ClassNotFoundException e) {
            e.printStackTrace();

        } catch(SQLException e) {
            e.printStackTrace();
        }finally {
            //释放资源
            if (rs != null) {
                try{
                    rs.close();
                } catch ( SQLException e) {
                    e.printStackTrace();
                }
            }
            if (ps != null) {
                try{
                    ps.close();
                } catch ( SQLException e) {
                    e.printStackTrace();
                }
            }

            if (cnn != null) {
                try{
                    cnn.close();
                } catch ( SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
